import { config } from '../config.js'
var QQMapWX = require('./qqmap-wx-jssdk.min.js');
class Login {
  constructor() {
    this.map = new QQMapWX({
      key: config.map_key
    });

  }
  login(_callback) {
    let _self = this;
    this.promise = new Promise(function (resolve, reject) {
      wx.login({
        success: function (res) {
            resolve(res);
        }
      });
    });

    this.promise.then(function (res) {
      return new Promise(function(resolve,reject){
      if (res.code) {
                    wx.request({
                      url: config.api_base_url + '/sys/getwxinfo',
                      data: {
                        code: res.code
                      },
                      success: function (res) {
                        let errCode = res.data.errCode;
                        if (errCode == '0000') {
                          let data = res.data.data[0];
                          wx.setStorageSync("sessionid", res.header["Set-Cookie"]);
                          resolve();
                          // wx.getLocation({
                          //   type: 'gcj02',
                          //   success: function (res) {
                          //     var latitude = res.latitude
                          //     var longitude = res.longitude
                          //     var location = {
                          //       latitude: latitude,
                          //       longitude: longitude
                          //     }
                          //     // _self.map.reverseGeocoder({
                          //     //   location: location,
                          //     //   success: function (res) {
                          //     //     let loginData = {
                          //     //       "mobileType":"",
                          //     //       "geographicLocation": res.result.address
                          //     //     }
                          //     //     wx.setStorageSync("location", loginData)
                          //     //     resolve()
                          //     //   },
                          //     //   fail: function (err) {
                                
                          //     //     resolve()
                          //     //   }
                          //     // })
                          //     resolve()
                          //   }
                          //})
                        } 
                      },
                      fail: function (error) {
                        console.log(error);
                      }
                    })
                  } else {
                    console.log('登录失败！' + res.errMsg);
                  }
      })
    }).then(function(){
        return new Promise(function(resolve,reject){
          let header;
          header = {
            'cookie': wx.getStorageSync("sessionid")//读取cookie
          };
          wx.showLoading({
            title: '登录中',
          })

          let loginData = {
            "mobileType": "",
            "geographicLocation": ""
          }
          let location = wx.getStorageSync("location");
          if (location != "") {
            loginData = location;
          }
          wx.request({
            url: config.api_base_url + '/usr/islogin',
            method: "POST",
            data: loginData,
            header: header,
            success: function (res) {
              let errCode = res.data.errCode;
              let errMsg = res.data.errMsg;
              if (errCode == '0000') {
                let data = res.data.data[0];
                console.log("获取ischecked",JSON.stringify(data));
                wx.setStorageSync("usercache", data);
                resolve()
              }else{
                resolve()
              }
            }
          })
        })
    }).then(function(){
      _callback();
    });
  }
}

export { Login }